#include "pub_log.h"


/***********************************************************************************
 *	函数名：WrLog_init
 *	功	能：日志初始化
 *	参	数：ulog ：日志信息
 *	返回值：0 成功， 小于0失败
 *
 ************************************************************************************/
int WrLog_init(log_t ulog)
{
    log_t *rlog = NULL;
    char loglevel[32];
    time_t t;
    char dtime[DTIME_LEN];
    char date[DTIME_LEN];
    char logFileName[FILE_NAME_MAX_LEN];

    memset(logFileName, 0x00, sizeof(logFileName));
    memset(dtime, 0x00, sizeof(dtime));
    memset(date, 0x00, sizeof(date));
    memset(loglevel, 0x00, sizeof(loglevel));

    GetTime(dtime, DTIME_LEN);
    memcpy(date, dtime, strlen(dtime));
    strtok(date, "|");

    rlog = getrlog();

    if(ulog.fd != NULL)
    {
        FClose(ulog.fd);
    }

    if(ulog.logpath == NULL || ulog.logpath[0] == '\0')
    {
        sprintf(ulog.logpath, "%s/log/", getenv("SWWORK"));
    }
    if(ulog.logname == NULL || ulog.logname[0] == '\0')
    {
        sprintf(ulog.logname, "%s", LOGNAME_DEFAULT);
    }
    memset(&ulog.ctime, 0x00, sizeof(ulog.ctime));
    ulog.ctime = localtime(&t);
    ulog.pid = getpid();

    sprintf(loglevel, "%s", LOGLEVELCONF_DEFAULT != NULL ? LOGLEVELCONF_DEFAULT : "0" );
    ulog.loglevel = atoi(loglevel);
    /*printf("DEBUG: loglevel = [%s] ulog.loglevel = [%d]\n", loglevel, ulog.loglevel);*/

    if(ulog.loglevel <= 0 )
    {
        ulog.loglevel = SW_LOG_ALL;
    }


    memcpy((void *)rlog, (void *)&ulog, sizeof(ulog));

    sprintf(logFileName, "%s%s_%s.log", rlog->logpath, rlog->logname, date);

    if ((rlog->fd = fopen(logFileName, "a+")) == NULL)
    {
        fprintf(stderr, " Open file %s error [%d] \n ", logFileName, errno);
        perror("fopen() :");
        return (-1);
    }

    WrLog(SW_LOG_INFO, "========================= BEGIN =========================");

    return 0;
}

int WrLog_init_by_logname_logpath(char *logname, char *logpath)
{
    log_t ulog;

    memset(&ulog, 0x00, sizeof(ulog));

    if(logname != NULL && logpath != NULL)
    {
        sprintf(ulog.logname, "%s", logname);
        sprintf(ulog.logpath, "%s", logpath);
    }

    return WrLog_init(ulog);
}
/***********************************************************************************
 *	函数名：WrLog
 *	功	能：写日志
 *	参	数：level ：日志级别
 *			pcFormat ：日志内容
 *	返回值：0 成功， 小于0失败
 *
 ************************************************************************************/
int WrLog(int level, char *fmt, ...)
{
    va_list ap;
    char msg[LOG_MAX_MSG_LEN];
    char dtime[DTIME_LEN];
    log_t *rlog = NULL;
    char slevel = '\0';

    memset(msg, 0x00, sizeof(msg));
    memset(&ap, 0x00, sizeof(ap));
    memset(dtime, 0x00, sizeof(dtime));

    va_start(ap, fmt);
    vsprintf(msg, fmt, ap);
    va_end(ap);
    rlog = getrlog();
    if(rlog->loglevel >= level)
    {
        /*printf("DEBUG: rlog->loglevel = [%d] level = [%d]\n", rlog->loglevel, level);*/
        GetTime(dtime, DTIME_LEN);
        get_level(level, &slevel);

        /*	@pid|dtime|level [__FILE__][__LINE__] msg	*/
        if (fprintf(rlog->fd, "@%d|%s|%c %s\n", rlog->pid, dtime, slevel, msg) < 0)
        {
            fprintf(stderr, " write file error [%d] \n " , errno);
            perror("fprintf():");
            return -1;
        }
    }
    return 0;
}
int slog_fflush()
{
    log_t *rlog = NULL;

    rlog = getrlog();
    if(rlog->fd > 0)
    {
        fflush(rlog->fd);
    }

    return 0;
}
int get_level(int level, char *slevel)
{
    switch(level)
    {
    case SW_LOG_INFO:
    {
        *slevel = 'I';
        break;
    }
    case SW_LOG_WARNING:
    {
        *slevel = 'W';
        break;
    }
    case SW_LOG_ERROR:
    {
        *slevel = 'E';
        break;
    }
    case SW_LOG_DEBUG:
    {
        *slevel = 'D';
        break;
    }
    default:
    {
        *slevel = 'O';
        break;
    }
    }
    return 0;
}
void WrLog_release(void)
{
    log_t *rlog = getrlog();

    WrLog(SW_LOG_INFO, "========================= END ===========================\n");
    FClose(rlog->fd);
    rlog = NULL;
    return ;
}

static int GetTime(char *dtime, int len)
{
    time_t t;
    struct tm *local_time;
    int year = 0;
    int mon = 0;
    int day = 0;
    int hour = 0;
    int min = 0;
    int sec = 0;

    if(dtime == NULL || len < DTIME_LEN )
    {
        printf("GetTime err\n");
        return -2;
    }
    memset(dtime, 0x00, len);

    t = time(0);
    local_time = localtime(&t);
    year = 1900 + local_time->tm_year;
    mon = local_time->tm_mon + 1;
    day = local_time->tm_mday;
    hour = local_time->tm_hour;
    min = local_time->tm_min;
    sec = local_time->tm_sec;
    sprintf(dtime, "%02d%02d%02d|%02d:%02d:%02d", year, mon, day, hour, min, sec);

    return 0;
}
int log_info(char *fmt, ...)
{
    va_list ap;
    char stmp[LOG_MAX_MSG_LEN];

    memset(stmp, 0x00, sizeof(stmp));

    va_start(ap, fmt);
    vsnprintf(stmp, LOG_MAX_MSG_LEN, fmt, ap);
    va_end(ap);
    WrLog(SW_LOG_INFO, stmp);
    return 0;
}
int log_debug(char *fmt, ...)
{
    va_list ap;
    char stmp[LOG_MAX_MSG_LEN];

    memset(stmp, 0x00, sizeof(stmp));

    va_start(ap, fmt);
    vsnprintf(stmp, LOG_MAX_MSG_LEN, fmt, ap);
    va_end(ap);
    WrLog(SW_LOG_DEBUG, stmp);
    return 0;
}
int log_err(char *fmt, ...)
{
    va_list ap;
    char stmp[LOG_MAX_MSG_LEN];

    memset(stmp, 0x00, sizeof(stmp));

    va_start(ap, fmt);
    vsnprintf(stmp, LOG_MAX_MSG_LEN, fmt, ap);
    va_end(ap);
    WrLog(SW_LOG_ERROR, stmp);
    return 0;

}

